/*
 * @Author: 高宇 gy15354315043@163.com
 * @Date: 2024-04-09 15:11:14
 * @LastEditors: 郝奔 1425466221@qq.com
 * @LastEditTime: 2024-12-18 10:30:38
 * @Description: 
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved. 
 */
import Cookies from 'js-cookie';
import { cloud } from '@/framework/utils/request';

const TokenKey = 'token';

export function getToken(): string | undefined {
  return Cookies.get(TokenKey);
}

export function setToken(token: string): void {
  Cookies.set(TokenKey, token);
}

export function removeToken(): void {
  return Cookies.remove(TokenKey);
}

export function imgpath(fileId: string): string {
  const filePath =
    import.meta.env.VITE_APP_BASE_API +
    '/sysBasic/sysFileInfo/previewByObjectName?fileId=' +
    fileId
  return filePath;
}
export function DownloadFileUrl(fileId: string, secretFlag?: string): string {
  if (secretFlag === 'Y') {
    const filePath =
      import.meta.env.VITE_APP_BASE_API +
      cloud.file +
      '/sysFileInfo/privateDownload?fileId=' +
      fileId +
      '&token=' +
      getToken();
    return filePath;
  } else {
    const filePath =
      import.meta.env.VITE_APP_BASE_API +
      cloud.file +
      '/sysFileInfo/publicDownload?fileId=' +
      fileId +
      '&token=' +
      getToken();
    return filePath;
  }
}
interface ObjType {
  username: string,
  password: string,
  checked: boolean
}
// 读取cookie
export function getCookie(): ObjType {
  const obj = {
    username: '',
    password: '',
    checked: false
  }
  if (document.cookie.length > 0) {
    const arr = document.cookie.split('; ')
    for (let i = 0; i < arr.length; i++) {
      const arr2 = arr[i].split('=')
      if (arr2[0] === 'username') {
        obj.username = arr2[1]
      } else if (arr2[0] === 'password') {
        obj.password = arr2[1]
      } else if (arr2[0] === 'state') {
        obj.checked = Boolean(arr2[1])
      }
    }
  }
  return obj
}
